<!-- 新增修改投标单位 -->
<template>
	<v-forms ref="formRef" :beforeHandler="(param: any) =>beforeHandler(param)" :hideFooter="!formState">
		<template #tint1="scope">
			<el-Divider content-position="left" style="margin: 5px 0px"
				><span style="color: #1a63db; font-weight: bold">{{ scope.data.item.label }}</span></el-Divider
			>
		</template>
		<template #org>
			<div style="display: flex; align-items: center">
				<div class="import" style="width: 118px; text-align: end; padding-right: 10px; font-size: 14px">{{ $t('34HDoV6Hki2Cjq7aCki1Z') }}</div>
				<el-cascader
					:disabled="!formState"
					style="width: 100%"
					v-model="selectOrgName"
					:options="orgData"
					@change="handleChange"
					:props="props"
					:show-all-levels="false"
					:placeholder="$t('jcAyqYrWKamW8uK_7Kbhp')"
				>
					<template #default="{ node }">
						<span :style="{ color: node.level === 1 ? 'gray' : 'inherit' }">
							{{ node.label }}
						</span>
					</template>
				</el-cascader>
			</div>
		</template>
		<template #n_EmpId>
			<div style="display: flex; align-items: center">
				<div class="import" style="width: 118px; text-align: end; padding-right: 10px; font-size: 14px">{{ $t('y7I9J0Du746oyZTnse2kM') }}</div>
				<el-select
					:disabled="!formState"
					style="width: 100%"
					v-model="selectPerName"
					filterable
					remote
					:placeholder="$t('jcAyqYrWKamW8uK_7Kbhp')"
					remote-show-suffix
					:remote-method="remoteMethod"
					@focus="remoteMethod"
				>
					<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
				</el-select>
			</div>
		</template>
		<template #levels="scope">
			<div>{{ scope.data.item[scope.data.item.key] }}</div>
			<multiple-styles-table-compoment :disabled="!formState" :table-config="discernDetailTableConfig" :scope="scope"> </multiple-styles-table-compoment>
		</template>
	</v-forms>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { discernFormConfig, VForms, discernDetailFormConfig } from '../config/form/index';
import { multipleStylesTableCompoment } from '/@/components/ys/index';
import { discernDetailTableConfig } from '../config/table/index';
import { useStaffInfoStore } from '/@/stores/ys/index';
import { ElMessage } from 'element-plus';
import { IPostDiscernType, IDiscernDetail } from '/@/types/ys/index';
import requestTotal from '/@/api/common/common';

import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
const { proxy } = useCurrentInstance();
const $t = proxy.$t;

const formRef = ref();
const staffInfoStore = useStaffInfoStore();
const orgData = ref(staffInfoStore.treeData);
const selectOrgName = ref([]); //选中的组织id
const selectPerName = ref(); //选中的负责人id

//currentChangeData,修改状态时的data,用来判断
let formState: any = ref(1); //当前form表单状态 0详情 1新增 2修改
let currentChangeData: any;

let postData: IPostDiscernType = {
	V_WorkType: '',
	N_Accident_TypeId: '',
	V_Inspection_Points: '',
	V_Control_Measures: '',
	N_OrgId: '',
	N_EmpId: '',
	N_isValid: 1,
	V_Remake: '',
	N_PushType: 1,
	levels: [],
}; //提交的给服务器的data

const beforeHandler = async (param: any) => {
	postData.levels = [];

	if (selectOrgName.value.length <= 1) {
		ElMessage.warning($t('key_3'));
		return Promise.resolve(false);
	}
	if (!selectPerName.value) {
		ElMessage.warning($t('key_4'));
		return Promise.resolve(false);
	}

	//
	console.log('提交');
	for (let i = 0; i < param.levels.length; i++) {
		let level = param.levels[i];
		for (let key in level) {
			// 如果对象属性的值不为空，就保存该属性（如果属性的值为0，保存该属性。如果属性的值全部是空格，属于为空。）
			// if (key == 'v_Shielding' && level[key].length == 0) {
			// 	ElMessage.warning($t('key_5'));
			// 	return Promise.resolve(false);
			// } else if (key == 'v_Advice_Shielding' && level[key].length == 0) {
			// 	ElMessage.warning($t('key_6'));
			// 	return Promise.resolve(false);
			// }

			if (key == 'v_WorkExplain' && !level[key]) {
				ElMessage.warning($t('key_7'));
				return Promise.resolve(false);
			} else if (key == 'v_Control_Level' && !level[key]) {
				ElMessage.warning($t('key_8'));
				return Promise.resolve(false);
			} else if (key == 'v_Control_Measures' && !level[key]) {
				ElMessage.warning('管控措施不能为空');
				return Promise.resolve(false);
			}
		}
		// level.v_Shielding = level.v_Shielding.join();
		// level.v_Advice_Shielding = level.v_Advice_Shielding.join();
		// let postDetail: IDiscernDetail = {
		// 	V_WorkExplain: level.v_WorkExplain,
		// 	V_Shielding: level.v_Shielding,
		// 	V_Control_Measures: level.v_Control_Measures,
		// 	V_Advice_Shielding: level.v_Advice_Shielding,
		// 	F_Risk_Assessment_L: level.f_Risk_Assessment_L,
		// 	F_Risk_Assessment_E: level.f_Risk_Assessment_E,
		// 	F_Risk_Assessment_C: level.f_Risk_Assessment_C,
		// 	V_Control_Level: level.v_Control_Level,
		// 	N_isValid: 1,
		// 	V_Remake: level.v_Remake,
		// 	n_Id: level.n_Id,
		// };

		let postDetail: IDiscernDetail = {
			V_WorkExplain: level.v_WorkExplain,
		
			V_Control_Measures: level.v_Control_Measures,
			F_Risk_Assessment_L: level.f_Risk_Assessment_L,
			F_Risk_Assessment_E: level.f_Risk_Assessment_E,
			F_Risk_Assessment_C: level.f_Risk_Assessment_C,
			V_Control_Level: level.v_Control_Level,
			N_isValid: 1,
			V_Remake: level.v_Remake,
			n_Id: level.n_Id,
		};

		postData.levels.push(postDetail);
	}

	postData.V_WorkType = param.v_WorkType;
	//修改状态,且事故类型值没改变
	postData.N_Accident_TypeId = formState.value == 2 && currentChangeData.v_Accient_TypeName == param.n_Accident_TypeId ? currentChangeData.n_Accident_TypeId : param.n_Accident_TypeId;

	// postData.V_Inspection_Points = param.v_Inspection_Points;  检查要点注释
	postData.V_Control_Measures = param.v_Control_Measures;
	postData.N_OrgId = selectOrgName.value[selectOrgName.value.length - 1];
	if (formState.value == 2 && currentChangeData.v_EmpName == selectPerName.value) {
		selectPerName.value = currentChangeData.n_EmpId;
	}
	postData.N_PushType = formState.value == 2 ? currentChangeData.N_PushType : 1;
	postData.N_EmpId = selectPerName.value;
	postData.N_isValid = param.n_isValid;
	postData.V_Remake = param.v_Remake;

	if (formState.value == 2) {
		postData.n_Id = currentChangeData.n_Id;
	}
	return Promise.resolve(true);
};

const props = {
	expandTrigger: 'hover' as const,
	checkStrictly: true,
};

//选择部门
const handleChange = (value: any) => {
	selectOrgName.value = value;
	selectPerName.value = '';
	remoteMethod('');
};

const options = ref([]);
//远程请求负责人
const remoteMethod = async (query: string) => {
	if (selectOrgName.value.length > 1) {
		const res = await requestTotal.getUserList({
			OrgId: selectOrgName.value[selectOrgName.value.length - 1],
			EmpName: query,
			IsChoose: false,
		});
		options.value = res.data;
	} else {
		ElMessage.warning($t('key_9'));
	}
};

//新增数据变更
const addNew = (data: any) => {
	selectOrgName.value = []; //选中的组织id
	selectPerName.value = ''; //选中的负责人id
	formState.value = 1;
	orgData.value = staffInfoStore.treeData;
	let list: any = discernFormConfig(data);
	formRef.value.openDialog(list, $t('key_10'));
};
//查看详情数据
const detail = (data: any) => {
	//TODO:待禁用select与详情table状态
	formState.value = 0;
	currentChangeData = JSON.parse(JSON.stringify(data));
	processData();
	currentChangeData.disable = true;
	let list: any = discernDetailFormConfig(currentChangeData);
	formRef.value.openDialog(list, $t('key_11'));
};

//修改数据
const change = (data: any) => {
	formState.value = 2;
	//对data数据进行处理,将levels中的 v_Advice_Shielding 与v_Control_Level 转成数组
	//深拷贝
	currentChangeData = JSON.parse(JSON.stringify(data));
	processData();
	let list: any = discernFormConfig(currentChangeData);
	formRef.value.openDialog(list, $t('key_12'));
};
//对服务器的数据进行加工后,展示
const processData = () => {
	orgData.value = staffInfoStore.treeData;
	console.log('当前数据', currentChangeData);
	selectPerName.value = currentChangeData.v_EmpName;
	selectOrgName.value = currentChangeData.pids;
	for (let i = 0; i < currentChangeData.levels.length; i++) {
		let level = currentChangeData.levels[i];
		//v_Shielding  v_Advice_Shielding字符串转数组
		// level.v_Shielding = level.v_Shielding.split(',');
		// level.v_Advice_Shielding = level.v_Advice_Shielding.split(',');
		postData.levels.splice(0, i, level);
	}
};

defineExpose({
	addNew,
	change,
	detail,
	postData,
});
</script>
<scope lang="scss" scoped></scope>
